﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi
{
    /// <summary>
    /// TYMeterData 的摘要说明：内部数据请求接口（数据监控）
    /// </summary>
    public class TYMeterData : IHttpHandler
    {
        string str_result = "";

        public void ProcessRequest(HttpContext context)
        {
            //application/json方式：即为JSON数据，不需要检查
            context.Response.ContentType = "application/json";

            //请求主体：JSON格式字符串
            string MeterIdList = "";

            Stream postData = context.Request.InputStream;
            if (postData.Length != 0)
            {
                StreamReader sRead = new StreamReader(postData);
                string postContent = sRead.ReadToEnd();

                sRead.Close();
                JObject json_post = (JObject)JsonConvert.DeserializeObject(postContent);
                
                MeterIdList = json_post["MeterIdList"].ToString();
                JArray MeterId = (JArray)JsonConvert.DeserializeObject(MeterIdList);
                string sql_MeterId = "";
                for (int i = 0; i < MeterId.Count; i++)
                {
                    if (sql_MeterId == "")
                    {
                        sql_MeterId = "('" + MeterId[i] + "'";
                    }
                    else
                    {
                        sql_MeterId += ",'" + MeterId[i] + "'";
                    }

                    if (i == MeterId.Count - 1)
                    {
                        sql_MeterId += ",'" + MeterId[i] + "')";
                    }
                }

                var MeterDataList = new ArrayList();
                string sql = "";

                sql = "select ybdz,xylx,zxlj,zxss,fxlj,fxss,txsj,fmzt,ybdy,sbdy,xhqz,snr,pci,sbcs,imei,imsi,gdyl from v_jk_yz_info where ybdz in ";
                sql += sql_MeterId;

                System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                string ybdz = "";                     //仪表地址
                string xylx = "";                     //协议类型
                string zxlj = "";                     //正向累计流量
                string zxss = "";                     //正向瞬时流量 
                string fxlj = "";                     //反向累计流量
                string fxss = "";                     //反向瞬时流量
                string txsj = "";                     //通讯时间
                string fmzt = "";                     //阀门状态
                string meter_fm = "";                 //阀门状态
                string ybdy = "";                     //仪表电压
                string sbdy = "";                     //设备电压
                string meter_dy = "";                 //仪表电压
                string xhqd = "";                     //信号强度
                string snr = "";                      //SNR值
                string pci = "";                      //PCI值
                string meter_xy = "";                 //通讯平台
                string sbcs = "";                     //水表、设备参数
                string imei = "";                     //IMEI编号
                string imsi = "";                     //IMSI编号
                string gdyl = "";                    //管道压力


                if (Meter_Data.Rows.Count > 0)
                {
                    for (int i = 0; i < Meter_Data.Rows.Count; i++)
                    {
                        ybdz = Meter_Data.Rows[i]["ybdz"].ToString();
                        xylx = Meter_Data.Rows[i]["xylx"].ToString();
                        zxlj = Meter_Data.Rows[i]["zxlj"].ToString();
                        gdyl = Meter_Data.Rows[i]["gdyl"].ToString();

                        if (xylx == "0" ||
                            xylx == "1" ||
                            xylx == "2" ||
                            xylx == "3" ||
                            xylx == "4" ||
                            xylx == "5" ||
                            xylx == "6" ||
                            xylx == "7" ||
                            xylx == "8" ||
                            xylx == "9" ||
                            xylx == "10")
                        {
                            zxss = "";
                            fxlj = "";
                            fxss = "";
                        }
                        else
                        {
                            zxss = Meter_Data.Rows[i]["zxss"].ToString();
                            fxlj = Meter_Data.Rows[i]["fxlj"].ToString();
                            fxss = Meter_Data.Rows[i]["fxss"].ToString();
                        }

                        txsj = Meter_Data.Rows[i]["txsj"].ToString();
                        fmzt = Meter_Data.Rows[i]["fmzt"].ToString();
                        if (fmzt == "阀开"|| fmzt== "Open")
                        {
                            meter_fm = "1";
                        }
                        else if (fmzt == "阀关" || fmzt == "Close")
                        {
                            meter_fm = "0";
                        }
                        else
                        {
                            meter_fm = "2";
                        }

                        ybdy = Meter_Data.Rows[i]["ybdy"].ToString();
                        if (ybdy == "0")
                        {
                            sbdy = Meter_Data.Rows[i]["sbdy"].ToString();
                            if (sbdy == "0")
                            {
                                sbdy = "";
                            }
                            else
                            {
                                meter_dy = sbdy;
                            }
                        }
                        else
                        {
                            meter_dy = ybdy;
                        }

                        xhqd = Meter_Data.Rows[i]["xhqz"].ToString();
                        snr = Meter_Data.Rows[i]["snr"].ToString();
                        pci = Meter_Data.Rows[i]["pci"].ToString();

                        if (xylx == "0" ||
                            xylx == "1")
                        {
                            meter_xy = "IOT";
                            imei = "";
                        }
                        else if (xylx == "2" ||
                                 xylx == "3" ||
                                 xylx == "4")
                        {
                            meter_xy = "UDP";
                            imei = Meter_Data.Rows[i]["imei"].ToString();
                        }
                        else if (xylx == "5" ||
                                 xylx == "6" ||
                                 xylx == "7")
                        {
                            meter_xy = "OneNet";
                            imei = Meter_Data.Rows[i]["imei"].ToString();
                        }
                        else if (xylx == "8" ||
                                 xylx == "9" ||
                                 xylx == "10")
                        {
                            meter_xy = "AEP";
                            imei = Meter_Data.Rows[i]["imei"].ToString();
                        }
                        else
                        {
                            meter_xy = "";
                            imei = Meter_Data.Rows[i]["imei"].ToString();
                        }
                        sbcs = Meter_Data.Rows[i]["sbcs"].ToString();
                        imsi = Meter_Data.Rows[i]["imsi"].ToString();

                        MeterDataList.Add(new
                        {
                            MeterId = ybdz,
                            PositiveCumulativeFlow = zxlj,
                            PositiveInstantaneousFlow = zxss,
                            AntiCumulativeFlow = fxlj,
                            AntiInstantaneousFlow = fxss,
                            CommunicationTime = txsj,
                            ValveStatus = meter_fm,
                            MeterVoltage = meter_dy,
                            SignalIntensity = xhqd,
                            SignalQuality = snr,
                            BaseStationNumber = pci,
                            MeterParameters = meter_xy,
                            CommunicationPlatform = sbcs,
                            IMEINumber = imei,
                            IMSINumber = imsi,
                            PipelinePressure = gdyl,
                        }
                        ) ;
                    };
                    var json_result = new
                    {
                        status = "success",
                        MeterDataList = MeterDataList,
                    };
                    str_result = WebSdkCore.ToJSON(json_result);
                }
                else
                {
                    var json_result = new
                    {
                        status = "success",
                        message = "No data",
                    };
                    str_result = WebSdkCore.ToJSON(json_result);
                }
            }
            else
            {
                var json_result = new
                {
                    status = "error",
                    message = "Missing parameter",
                };
                str_result = WebSdkCore.ToJSON(json_result);
            }
            context.Response.Write(str_result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}